package com.growthsee.rider.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.growthsee.common.core.controller.BaseController;
import com.growthsee.common.core.domain.AjaxResult;
import com.growthsee.rider.domain.RiderAuditResult;
import com.growthsee.rider.domain.RiderInfo;
import com.growthsee.rider.domain.RiderInfoCar;
import com.growthsee.rider.service.IRiderAuditResultService;
import com.growthsee.rider.service.IRiderInfoCarService;
import com.growthsee.rider.service.RiderInfoService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 【请填写功能名称】Controller
 *
 * @author growthsee
 * @date 2021-08-22
 */
@RestController
@RequestMapping("/rider/result")
public class RiderAuditResultController extends BaseController
{
    @Autowired
    private IRiderAuditResultService riderAuditResultService;
    @Autowired
    private RiderInfoService riderInfoService;
    @Autowired
    private IRiderInfoCarService riderInfoCarService;

    @ApiOperation("审核骑手")
    @PostMapping
    @ApiImplicitParam(name = "riderCompany", value = "公司信息", dataType = "RiderCompany")
    public AjaxResult insert(@RequestBody RiderAuditResult auditResult) {
        String checkResult = check(auditResult);
        if(StringUtils.isNotEmpty(checkResult)){
            return AjaxResult.error(checkResult);
        }
        riderAuditResultService.save(auditResult);
        //更新骑手状态和电瓶车审核状态
        riderInfoService.update(Wrappers.<RiderInfo>lambdaUpdate()
                .set(RiderInfo::getAuditStatus,auditResult.getQshc())
                .eq(RiderInfo::getId,auditResult.getRyid())
        );
        //更新电瓶车审核状态
        riderInfoCarService.update(Wrappers.<RiderInfoCar>lambdaUpdate()
                .set(RiderInfoCar::getAuditStatus,auditResult.getQshc())
                .eq(RiderInfoCar::getRiderId,auditResult.getRyid())
        );
        return AjaxResult.success();
    }

    /**
     * 检查数据
     * @param auditResult
     * @return
     */
    private  String check(RiderAuditResult auditResult){
        if (StringUtils.isEmpty(auditResult.getSfzmhm())){
            return "身份证号码不能为空";
        }
        if (StringUtils.isEmpty(auditResult.getQshc())){
            return "骑手核查结果不能为空";
        }
        if (StringUtils.isEmpty(auditResult.getClhc())){
            return "车俩核查结果不能为空";
        }
        if (StringUtils.isEmpty(auditResult.getRyid())){
            return "骑手id不能为空";
        }
        return "";
    }
}
